package com.ck.roomdemo;

import android.content.Context;

import androidx.lifecycle.LiveData;

import com.ck.roomdemo.room.MyDatabase;
import com.ck.roomdemo.room.Student;
import com.ck.roomdemo.room.StudentDao;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyRepository {
    private StudentDao studentDao;
    private MyDatabase myDatabase;
    private ExecutorService executorService;

    public LiveData<List<Student>> getLiveData() {
        return liveData;
    }

    private LiveData<List<Student>> liveData;

    public MyRepository(Context context) {
        executorService = Executors.newFixedThreadPool(1);
        myDatabase = MyDatabase.getDatabase(context);
        studentDao = myDatabase.getStudentDao();
        liveData = studentDao.getAll();
    }


    public void clear() {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                studentDao.deleteAll();
            }
        });
    }
    public void delete(Student student) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                studentDao.delete(student);
            }
        });
    }

    public void update(Student student) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                studentDao.update(student);
            }
        });
    }

    public void insert(Student...students) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                studentDao.insert(students);
            }
        });
    }
}
